package net.oschina.aibinxiao.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import net.oschina.aibinxiao.dao.UserDao;
import net.oschina.aibinxiao.domain.User;
import net.oschina.aibinxiao.util.JDBCUtils;

/**
 * UserDao实现类
 * @author aibinxiao
 * @date 2017年11月8日 下午4:41:06
 */
public class UserDaoImpl implements UserDao {

	/**
	 * 保存用户数据
	 * @throws Exception 
	 */
	@Override
	public void regist(User user) throws Exception {
		QueryRunner queryRunner = JDBCUtils.getQueryRunner();
		String sql = "insert into user values (?,?,?,?,?,?,?,?)";
		Object[] params = {user.getUid(), user.getUsername(), user.getPassword(), 
				user.getNickname(), user.getEmail(), user.getState(), user.getCode(), user.getRegisterTime()};
		queryRunner.update(sql, params);
	}

	/**
	 * 根据code查询用户
	 */
	@Override
	public User findByEmail(String email) throws Exception {
		QueryRunner queryRunner = JDBCUtils.getQueryRunner();
		String sql = "select * from user where email = ?";
		User user = queryRunner.query(sql, new BeanHandler<User>(User.class), email);
		return user;
	}

	/**
	 * 更新用户数据
	 */
	@Override
	public void update(User user) throws Exception {
		QueryRunner queryRunner = JDBCUtils.getQueryRunner();
		String sql = "update user set state=?,code=? where uid= ?";
		Object[] params = {user.getState(), user.getCode(), user.getUid()};
		queryRunner.update(sql, params);
	}

}
